//ajax请求
(function (w) {
    var time;
    var netWork = function (data,method) {
           if(data.loading) data.loading();//显示加载中
           //跨域测试
            // var script = document.createElement('script');
            // script.type = "text/javascript";
            // script.src = data.url+"&callback="+method;
            // document.head.appendChild(script);
            // return;
           //正常请求
            try {
                var xmlhttp;
                if (window.XMLHttpRequest){
                    xmlhttp=new XMLHttpRequest();
                }
                else {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        if(data.endLoading) data.endLoading();//结束加载中
                        if(method) method(xmlhttp.responseText);
                    }
                }

                if(data.method && data.method == "post"){
                    xmlhttp.open("post",data.url,data.sync?false:true);
                    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置表单类型
                    xmlhttp.send(data.data);
                }else{
                    xmlhttp.open("get",data.url,data.sync?false:true);
                    xmlhttp.send();
                }
                time = setTimeout(function(){
                    if(data.prize && xmlhttp.readyState!=4 || xmlhttp.status!=200){//抽奖按钮控制
                        xmlhttp.abort();
                        if(data.endLoading) data.endLoading();//结束加载中
                        noNetwork();
                        if(data.thisObj)data.thisObj.buttonControl = false;
                    }
                },6000);
                //setTimeout(function(){
                //    if(data.prize && !xmlhttp.statusText){
                //        if(document.getElementById("toast")){
                //            xmlhttp.abort();
                //            if(data.endLoading) data.endLoading();//结束加载中
                //            document.getElementById("toast").innerText = "亲，请检查你的网络君～";//
                //            document.getElementById("toast").className += " toasts";
                //            clearTimeout(time);
                //            if(data.thisObj)data.thisObj.buttonControl = false;
                //            return false;
                //        }
                //    }
                //},800);

                //xmlhttp.open("GET","login.jsp",true);
                //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//multipart/form-data application/x-www-form-urlencoded
                //xmlhttp.send("upload=451");
            } catch(e) {
                console.log(e.name +" "+ e.message);
            } finally {

            }
    },
   //获取参数key值
    getQueryString = function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = decodeURI(window.location.search).substr(1).match(reg);
        if (r != null) return unescape(r[2]); return "";
    },
        //无网络，自己调用
    noNetwork = function(){
        if(document.getElementById("toast")){
            document.getElementById("toast").innerText = "亲，请求超时～";//亲，请检查你的网络君～
            document.getElementById("toast").className += " toasts";
        }
    },
    //图片加载错误处理
    imgLoadError = function(obj){
        obj.src = "/pages/static/mobile/images/portrait_normal.jpg";//默认头像
    },
    numTowDisplay = function(num){
        if(num < 10 && num > 0){
            num = "0"+num;
        }
        return num;
    };

    w.getQueryString = getQueryString;
    w.netWork = netWork;//ajax
    w.imgLoadError = imgLoadError;
    w.numTowDisplay = numTowDisplay;
})(window);
window.addEventListener("offline" , function()
{
    if(document.getElementById("toast")){
        document.getElementById("toast").innerText = "亲，请检查你的网络君～";//
        document.getElementById("toast").className += " toasts";
    }
} , true);
